package com.zw.mes.module.system.controller;

import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.common.ValidResult;
import com.zw.mes.entity.sys.Menu;
import com.zw.mes.entity.sys.Role;
import com.zw.mes.module.system.service.MenuService;
import com.zw.mes.module.system.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 角色接口
 *
 * 权限：
 * system:role:view 查看
 * system:role:edit 编辑
 *
 * @Author: China.sgl
 * @Date: 2019/9/16 09:11
 */
@Controller
@RequestMapping("/sys/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;

    @GetMapping
    @RequiresPermissions({"system:role:view"})
    public String page() {
        return "system/role/page";
    }

    /** 系统角色数据 */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"system:role:view"})
    public List<Role> list(Role role) {
        return roleService.findRoleByCondition(role);
    }

    /** 删除角色 */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"system:role:edit"})
    public JsonResult del(Integer id) {
        return jsonResult(roleService.delRole(id));
    }

    /** 角色名唯一验证 */
    @ResponseBody
    @PostMapping("/checkUnique")
    @RequiresPermissions({"system:role:view"})
    public ValidResult checkUnique(Role r) {
        Role role = roleService.findRoleByName(r.getName());
        if (r.getId() == null) {
            return ValidResult.result(role);
        }
        if (role == null || role.getId().equals(r.getId())) {
            return ValidResult.SUCCESS;
        }
        return ValidResult.FAIL;
    }

    /** 创建角色 */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"system:role:edit"})
    public JsonResult save(Role role, Integer[] menuIds) {
        return isNotNull(roleService.saveRole(role, menuIds));
    }

    /** 修改角色与权限 */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"system:role:edit"})
    public JsonResult update(Role role, Integer[] menuIds) {
        return isNotNull(roleService.updateRole(role, menuIds));
    }

    /** 查询所有权限 */
    @GetMapping("/menu")
    @ResponseBody
    @RequiresPermissions({"system:role:view"})
    public List<Menu> menu() {
        return menuService.findAll();
    }

    /** 查询所有权限，标识角色拥有权限 */
    @GetMapping("/menu/{id}")
    @ResponseBody
    @RequiresPermissions({"system:role:view"})
    public List<Menu> menu(@PathVariable Integer id) {
        return menuService.findMenuCheckedByRoleId(id);
    }

    /** 查询角色 */
    @ResponseBody
    @PostMapping("/id")
    @RequiresPermissions({"system:role:view"})
    public Role id(Integer id) {
        return roleService.findRoleById(id);
    }

}
